package com.lgkj.ui.meitao;

import android.content.Context;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lgkj.adapter.SearShopAdapter;
import com.lgkj.bean.SearchShopBean;
import com.lgkj.bean.meitao.GoodsTypeBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/21 0021.
 */

public class HomeFragmentUtils {
    private Context context;
    private SearShopAdapter searShopAdapter;

    public HomeFragmentUtils(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        searShopAdapter = new SearShopAdapter(context);
    }

    public void getShopType() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(context));
        HttpUtils.postHttp(map, CommonConstant.goodsTypeList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    GoodsTypeBean goodsTypeBean = new Gson().fromJson(String.valueOf(jsonObject), GoodsTypeBean.class);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    public void search(String searchName, final ResultCallBack  callBack) {
        CommonUtils.showDiaLog(context, "正在查询");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(context));
        map.put("search_data", searchName);
        HttpUtils.postHttp(map, CommonConstant.searchShop, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                CommonUtils.dismissDiaLog();
                Toast.makeText(context, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                Gson mGson = new Gson();
                if (jsonObject.optInt("code") == 0) {
                    SearchShopBean searchShopBean = mGson.fromJson(String.valueOf(jsonObject), SearchShopBean.class);
                    if (searchShopBean != null && searchShopBean.getData() != null && searchShopBean.getData().getData() != null) {
                        callBack.succese(searchShopBean.getData().getData());
                    }
                }

            }

            @Override
            public void errorHttp(String msg) {
                CommonUtils.dismissDiaLog();
            }
        });
    }

    interface ResultCallBack {
        void succese(Object object);
    }
}
